package com.example.WeChat.Control.Api;
import com.example.WeChat.Control.Response.RedisClientTemplate;
import com.example.WeChat.Control.Response.Status;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.boot.web.servlet.error.ErrorController;


import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

@Controller
public class CustomErrorController implements ErrorController {

    private static final String PATH = "/error";
    private static final Logger logger = LogManager.getLogger(CustomErrorController.class);

    @RequestMapping(value = PATH)
    @ResponseBody
    public RedisClientTemplate<Object> error(HttpServletRequest request) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        int statusCode = Integer.parseInt(status.toString());
        // 返回 JSON 格式的响应，可以包含自定义的重定向信息
        logger.error("服务器状态码:"+statusCode);
        return new RedisClientTemplate<>(statusCode, Status.Error,"",null);
    }
}
